package Dao.iml;

import Dao.AirInfoDao;
import DbUtil.DbUtils;
import bean.AirInfo;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class AirInfoDaoImpl extends DbUtils implements AirInfoDao {

    @Override
    public List<AirInfo> getAllInfo() {
        String sql = "select * from airinfo";
        List<AirInfo> airInfos = new ArrayList<>();
        ResultSet resultSet = query(sql, airInfos);
        try {
            while (resultSet.next()) {
                AirInfo airInfo = new AirInfo();
                airInfo.setId(resultSet.getInt("id"));
                airInfo.setNum(resultSet.getString("num"));
                airInfo.setAddress(resultSet.getString("address"));
                airInfo.setDate(resultSet.getString("date"));
                airInfos.add(airInfo);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeAll();
        }
        return airInfos;
    }

    @Override
    public List<AirInfo> getByDate(String date) {
        String sql = "select * from airinfo where date=?";
        List list = new ArrayList<>();
        list.add(date);
        ResultSet resultSet = query(sql, list);
        List<AirInfo> airInfos = new ArrayList<>();
        try {
            while (resultSet.next()) {
                AirInfo airInfo = new AirInfo();
                airInfo.setId(resultSet.getInt("id"));
                airInfo.setNum(resultSet.getString("num"));
                airInfo.setAddress(resultSet.getString("address"));
                airInfo.setDate(resultSet.getString("date"));
                airInfos.add(airInfo);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeAll();
        }
        return airInfos;
    }

    @Override
    public List<AirInfo> getByAddress(String address) {
        String sql = "select * from airinfo where address=?";
        List list = new ArrayList<>();
        list.add(address);
        ResultSet resultSet = query(sql, list);
        List<AirInfo> airInfos = new ArrayList<>();
        try {
            while (resultSet.next()) {
                AirInfo airInfo = new AirInfo();
                airInfo.setId(resultSet.getInt("id"));
                airInfo.setNum(resultSet.getString("num"));
                airInfo.setAddress(resultSet.getString("address"));
                airInfo.setDate(resultSet.getString("date"));
                airInfos.add(airInfo);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeAll();
        }
        return airInfos;
    }

    @Override
    public boolean deleteByNum(String num) {
        String sql = "delete from airinfo where num=?";
        List list = new ArrayList<>();
        list.add(num);
        int i = update(sql, list);
        if (i>0){
            return true;
        }
        return false;
    }

    @Override
    public boolean updateByNum(String num,AirInfo airInfo) {
        String sql = "update airinfo set id=?,num=?, address=?,date=? where num=?";
        List list = new ArrayList<>();
        list.add(airInfo.getId());
        list.add(airInfo.getNum());
        list.add(airInfo.getAddress());
        list.add(airInfo.getDate());
        list.add(num);
        int i = update(sql, list);
        if (i>0){
            return true;
        }
        return false;
    }

    @Override
    public ResultSet findByNum(String num) {
        String sql = "select * from airinfo where num=?";
        List list = new ArrayList<>();
        list.add(num);
        ResultSet resultSet = query(sql, list);
        return resultSet;
    }
}
